<?php
namespace App\Shop\Logic;

use App\Models\SwtMerchant;
use App\Models\SwtMerchantMachine;
use App\Models\SwtMerchantProfit;

class MerchantLogic extends \App\Http\Logic\BaseLogic
{
    /**
     * @notes 获取用户货款
     * @param $user_id
     * @return boolean|array
     * @author Evil丿
     * @date 2024/11/25 16:42
     */
    public static function getUserDepositNow($user_id)
    {
        $info = SwtMerchant::firstWhere('shop_user_id', $user_id);

        if(!$info) {
            return false;
        }

        $profit_virtual = $info->virtual_device_num * $info -> deposit;

        $deposit_now = SwtMerchantMachine::where('merchant_id', $info -> id) -> where('is_owner', 1) -> sum('deposit_now');

        return [
            'deposit_now'    => bcadd($deposit_now, $profit_virtual, 2),
            'profit_virtual' => $profit_virtual,
            'now'            => $deposit_now,
            'user_id'        => $user_id
        ];
    }
    /**
     * @notes 获取用户ID
     * @param $user_id
     * @return mixed
     * @author Evil丿
     * @date 2024/08/07 14:59
     */
    public static function getMerchantIdById($user_id)
    {
        return SwtMerchant::where('shop_user_id', $user_id) -> value('id');
    }

    /**
     * @notes 获取用户信息
     * @param $user_id
     * @return mixed
     * @author Evil丿
     * @date 2024/08/07 17:16
     */
    public static function getMerchantSaleById($user_id)
    {
        $user_info = SwtMerchant::firstWhere('shop_user_id', $user_id);

        if(!$user_info) {
            self::$error = "用户不存在";
            return false;
        }
        return $user_info;
    }

    /**
     * @notes 获取用户数据
     * @param $user_id
     * @return false|float[]
     * @author Evil丿
     * @date 2024/11/22 10:14
     */
    public static function getSaleById($user_id)
    {
        $merchant_info = self::getMerchantSaleById($user_id);

        if(!$merchant_info) {
            return false;
        }

        $merchant_id = $merchant_info -> id;

        $date = date('Y-m-d', time());

        $today_profit_amount_add = SwtMerchantProfit::where('merchant_id', $merchant_id)
            -> where('type',0)
            -> whereDate('created_at', $date)
            -> sum('profit');

        $today_profit_amount_sub = SwtMerchantProfit::where('merchant_id', $merchant_id)
            -> where('type',1)
            -> whereDate('created_at',$date)
            -> sum('profit');

        $today_sale_amount_add = SwtMerchantProfit::where('merchant_id', $merchant_id)
            -> where('type',0)
            -> whereDate('created_at',$date)
            -> sum('amount');

        $today_sale_amount_sub = SwtMerchantProfit::where('merchant_id', $merchant_id)
            -> where('type',1)
            -> whereDate('created_at',$date)
            -> sum('amount');


        $profit_amount_add = SwtMerchantProfit::where('merchant_id', $merchant_id)
            -> where('type',0)
            -> sum('profit');

        $profit_amount_sub = SwtMerchantProfit::where('merchant_id', $merchant_id)
            -> where('type',1)
            -> sum('profit');

        $sale_amount_add = SwtMerchantProfit::where('merchant_id', $merchant_id)
            -> where('type',0)
            -> sum('amount');

        $sale_amount_sub = SwtMerchantProfit::where('merchant_id', $merchant_id)
            -> where('type',1)
            -> sum('amount');

        $data = [
            'amount_sale'   => (float) bcsub($sale_amount_add / 100, $sale_amount_sub / 100, 2),
            'today_sale'    => (float) bcsub($today_sale_amount_add / 100, $today_sale_amount_sub / 100, 2),
            'amount_profit' => (float) bcsub($profit_amount_add / 100, $profit_amount_sub / 100, 2),
            'today_profit'  => (float) bcsub($today_profit_amount_add / 100, $today_profit_amount_sub / 100, 2),
        ];

        return $data;
    }
}
